/*
bea_u920.h
offer bea u920 lidar data type define and function api
designed by chenwenbo

----------------------------
2020-11-18 10:00:00
create file
----------------------------
*/
#ifndef __BEA_U920_H
#define __BEA_U920_H

#include "../../bsp/base_type.h"
#include "../../../include/aos/hal/uart.h"

/*LZR-MicroScan Raw Data Communication Protocol (Doc: E303-MicroScan RawData Version V1.3 2019-11-14)*/

/*CMD define*/
#define CMD_STE_PARAMETERS			(0xC353)
#define CMD_GET_PARAMETERS			(0xC354)
#define CMD_STORE_PARAMETERS		(0xC355)
#define CMD_GET_IDENTITY			(0xC35A)
#define CMD_GET_MEASUREMENTS		(0xC35B)
#define CMD_RESET_MDI_COUNTER		(0xC35E)
#define CMD_RESET_HEARTBEAT_COUNTER	(0xC35F)
#define CMD_RESET_EMERGENCY_COUNTER	(0xC361)
#define CMD_SET_LED					(0xC378)
#define CMD_HEARTBEAT				(0xC364)
#define CMD_EMERGENCY				(0xC35B)

#define SAMPLING_PERIOD_MS		(50)
#define DISTANCE_MAX_MM			(10000)

#define PI 3.1415926
#define BEA_dir 0
#define BEA_key 0x90d9

#define GET_CMD(buf)	(buf[11] | (buf[12] << 8))

/*Fram DATA filed structure*/

typedef struct{
	u16	angle_first_facet;		//Angle_first: limit of the detection facet_x ntc_inFrame
	u16	angle_last_facet;		//Angle_last: limit of the detection facet_x ntc_inFrame
	u16	spots_number_facet;		//Spots_number: limit of the detection facet_x(max 250)
}Type_PlaneInfo_Def;

typedef struct{
	u8 	d0;						//Reserved for future use, write 0
	
	u8	ntc_inFrame;			//CTN field in measurement frames (temperature): 0 : disable 1 : enable 
	
	u8	meas_info;				//0 : send MDI only (each spots distance value occupy 2bytes)
								//1 : send MRI (each spots pulse width value occupy 2bytes)
								//2 : send MDI and MRI (each spots pulse width value occupy 4bytes)
								//3 : send MDI and MRI in condense mode(Peco version, distance and
								//pulse width occupy 2bytes; 5 first bits = pulseWidth and 11 last bits =
								//distance, distance is expressed in cm and pulseWidth is divided by 128.)
	
	u8	d3_d13[11];				//Reserved
	
	Type_PlaneInfo_Def plane[4];//plane struct, (angle_first, angle_last, spots_number)
	
	u8	count_inFrame;			//MDI frame counter in measurement. 0 : disable 1 : enable
	
	u8	can_inFrame;			//CAN in frame. 0 : disable 1 : enable
	
	u8	heartbeat_period;		//Heartbeat period (unit : 1 sec). 
								//Range : 0 to 255. If the value is 0, the heartbeat is disabled
								
	u8	facet_id_inFrame;		//Facet number filed in MDI. 0 : disable 1 : enable

	u8	baudrate_sel;			//Baudrate :
								//	0 : 57600
								//	1 : 115200
								//	2 : 230400
								//	3 : 460800
								//	4 : 921600
}Type_Set_Parameters_Def;


typedef struct{
	union
	{
		u32 d0_d3;
		struct{
			u32	reservd0			: 1;	// 0, reserved for future use
			u32	ntc_inFrame			: 1;	// enable CTN
			u32	meas_info			: 1;	// measurement information (MDI, MRI, or both)
			u32	reservd3_8			: 6;	// 0, reserved
			u32	detection_spots_num	: 1;	// number of spots in the detection field 
			u32	reservd10_11		: 2;	// 0, reserved for future use
			u32	angle_first			: 1;	// angle_first
			u32	angle_last			: 1;	// angle_last
			u32	count_inFrame		: 1;	// mdi frame counter fields
			u32	can_inFrame			: 1;	// can number fields
			u32	heartbeat_period	: 1;	// heartbeat period
			u32	facet_id_inFrame	: 1;	// facet field
			u32	reservd18_31		: 14;	// 0, reserved for future use 
		}Bit;
	};
	
	u16	communication_per;		//Communication charge in %, computed based on the quantity of data
								//and the period of the transmission. If the charge is more than 100%, it is
								//not possible to transmit all information. The charge should be less than 70%.
	
	u8	d6;						//0, reserved for future use. 
	
	u8	ntc_inFrame;			//CTN field in measurement frames (temperature): 0 : disable 1 : enable 
	
	u8	meas_info;				//0 : send MDI only (each spots distance value occupy 2bytes)
								//1 : send MRI (each spots pulse width value occupy 2bytes)
								//2 : send MDI and MRI (each spots pulse width value occupy 4bytes)
								//3 : send MDI and MRI in condense mode(Peco version, distance and
								//pulse width occupy 2bytes; 5 first bits = pulseWidth and 11 last bits =
								//distance, distance is expressed in cm and pulseWidth is divided by 128.)
	
	u8	d9_d13[5];				//Reserved
	
	Type_PlaneInfo_Def plane[4];//plane struct, (angle_first, angle_last, spots_number)
	
	u8	count_inFrame;			//MDI frame counter in measurement. 0 : disable 1 : enable
	
	u8	can_inFrame;				//CAN in frame. 0 : disable 1 : enable
	
	u8	heartbeat_period;		//Heartbeat period (unit : 1 sec). 
								//Range : 0 to 255. If the value is 0, the heartbeat is disabled
								
	u8	face_id__inFram;		//Facet number filed in MDI. 0 : disable 1 : enable

	u8	baudrate_sel;			//Baudrate :
								//	0 : 57600
								//	1 : 115200
								//	2 : 230400
								//	3 : 460800
								//	4 : 921600
}Type_Get_Parameters_Def;

typedef struct{
	u32	part_number;			//Product part number (BEA TOF), for example 20077201
	u8	software_version;		//Software version
	u8	software_revision;		//Software prototype
	u8	d7_d10[4];				//Reserved
	u8	d11_d14[4];				//CAN number of the detector (RPU BEA serial number)
	u8	d15_d18[4];				//RPU config number
	u8	d19_d42[24];			//Laser head Config number.
								//DWORD softNumber_dw;
								//DWORD softCanNumber_dw;
								//DWORD tofNumber_dw;
								//DWORD cfgNumber_dw;
								//WORD softVersion_w;
								//WORD tofVersion_w;
								//WORD cfgVersion_w;
								//WORD softProto_w;

}Type_Recv_Identity_Def;

typedef struct{
	u8 	meas_mode;				//0 : single shot	1 : continuous
}Type_Get_Measurements_Def;

typedef struct{
	u8	isUpdate;
	u16	limit[4];
	u16 Data[250];
	u16	spotSize;
}Type_Plane_Def;

typedef struct{
	u8 *pData;
	u16	size;
}Type_Frame_Data_Def;

typedef struct{
	u8	state;
	u32	timeStamp;
	
	u8 	isProcess;
	
	uart_dev_t uart;
	
	u16	temperture;
	u8	facet;
	u8	canBuf[8];
	u16 count;
	
	Type_Plane_Def			plane[4];
	Type_Frame_Data_Def 	frameRecv;
	Type_Frame_Data_Def		frameProcess;
	Type_Set_Parameters_Def paramSet;
	Type_Get_Parameters_Def	paramGet;
	Type_Recv_Identity_Def	identity;
}Type_Bea_U920_Def;

/*********************************function extern************************/
extern int xp_bea_u920_init(void *arg, u8 port,u32 speed);   //init lidar


#endif